package lovo.practice_7_14;

import lovo.Util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetPanel extends JPanel implements ActionListener {
    private JTextField jTextFieldGet;
    private JButton jButtonGet;
    private J189_7_14 frame;
    private Icon imgback = Util.getimagesIcon("/Images/buttonOff.jpg");
    private JButton jButtonBack;
    private Icon ImageGet = Util.getimagesIcon("/Images/buttonGet.jpg");

    public GetPanel(J189_7_14 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jButtonGet = new JButton(ImageGet);
        jButtonGet.setBounds(200, 200, 140, 50);
        this.add(jButtonGet);
        jButtonGet.addActionListener(this);

        jTextFieldGet = new JTextField();
        jTextFieldGet.setBounds(400, 200, 140, 50);
        this.add(jTextFieldGet);

        jButtonBack = new JButton(imgback);
        jButtonBack.setBounds(500, 400, 140, 50);
        this.add(jButtonBack);
        jButtonBack.addActionListener(this);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtonBack) {
            frame.switchPanel("mune");
        } else if (e.getSource() == jButtonGet) {
            if (frame.getUsers().getBalance() >= Integer.parseInt(frame.getGetPanel().jTextFieldGet.getText())) {
                frame.getUsers().setBalance(frame.getUsers().getBalance() - Integer.parseInt(frame.getGetPanel().jTextFieldGet.getText()));
                JOptionPane.showMessageDialog(this, "取款成功");
            } else {
                JOptionPane.showMessageDialog(this, "钱不够，阎王贷了解一下");
            }
            frame.getGetPanel().jTextFieldGet.setText("");

        }
    }
}
